home *** CD-ROM | disk | FTP | other *** search
- /*
- Copyright © 1990 by Succinct Systems
-
- 433 Huronview
- Ann Arbor, MI 48103
- (313) 663-4903
-
- File: PolyUtils.c
- Model: THINK C 4.0, MPW C 3.0
-
- ABSTRACT:
- generate regular polygons and lists of endpoints
-
- NOTES:
- none yet.
-
- KNOWN BUGS:
- none yet.
-
- HISTORY:
- DAS 10-Jun-90 created this file (David A. Surovell)
- */
-
- #include <StdMacIncludes.h> /* include standard Mac headers */
- #include <Compatibility.h> /* define compatibility control structures */
- #include <SANE.h>
- #include "PolyUtils.h"
-
-
- /*
- ** create a regular polygon;
- ** center point is ( vertexRadius, vertexRadius ).
- **
- ** arguments:
- ** thisPoly: polygonal result
- ** sideCount: number of sides
- ** vertexRadius: length from center to vertex
- ** degreeOffset: start and this number of degrees from 0°
- **
- ** return:
- ** noErr: success
- ** -1: bad arguments
- ** (other): MemoryMgr error
- */
- OSErr CreateRegularPoly(
- PolyHandle *thisPoly,
- short sideCount,
- short vertexRadius,
- short degreeOffset )
- {
- PolyHandle localPH;
- Point *vertices;
- short i;
- OSErr errStat;
-
- if (thisPoly == NULL)
- return (OSErr)(-1);
-
- *thisPoly = NULL;
-
- /* obtain the list of polygon endpoints */
- errStat = CreateVertexList( &vertices, sideCount, vertexRadius, degreeOffset );
- if (errStat != noErr)
- return errStat;
-
- /* create the polygon itself */
- localPH = OpenPoly();
- MoveTo( vertices[0].h, vertices[0].v );
- for (i=1; i<sideCount; i++)
- LineTo( vertices[i].h, vertices[i].v );
-
- LineTo( vertices[0].h, vertices[0].v );
- ClosePoly();
-
- /* cleanup and report */
- DisposPtr( vertices );
- if (errStat == noErr)
- *thisPoly = localPH;
-
- return errStat;
- }
-
-
- /*
- ** calculate a list of endpoints for a regular polygon;
- ** center point is ( vertexRadius, vertexRadius ).
- **
- ** arguments:
- ** thisPoly: polygonal result
- ** sideCount: number of sides
- ** vertexRadius: length from center to vertex
- ** degreeOffset: start and this number of degrees from 0°
- **
- ** return:
- ** noErr: success
- ** -1: bad arguments
- ** (other): MemoryMgr error
- */
- OSErr CreateVertexList(
- PointPtr *pointList,
- short sideCount,
- short vertexRadius,
- short degreeOffset )
- {
- register Point *vertices;
- register short i;
- register Fixed trigFixer, radiusFixer;
- register OSErr errStat;
-
- #ifdef _MC68881_
- extended curFloatCoord;
- double curDegrees, curRadians;
- #else
- extended curFloatCoord, curDegrees, curRadians;
- #endif _MC68881_
-
- if ((pointList == NULL) || (sideCount < 3) || (vertexRadius < sideCount))
- return (OSErr)(-1);
-
- /* force “degreeOffset” into sensible range */
- while (degreeOffset < 0)
- degreeOffset += 360;
- degreeOffset %= 360;
-
- pointList = NULL;
- vertices = (Point*)NewPtr( sideCount * sizeof(Point) );
- errStat = MemError();
- if (errStat != noErr)
- return errStat;
-
- /* create the a list of vertex points */
- radiusFixer = (long)vertexRadius << 16;
- for (i=0; i<sideCount; i++)
- {
- /* calculate the degree position of the endpoint, */
- /* and convert it into radians */
- curDegrees = degreeOffset + ((i * 360.0) / (sideCount * 1.0));
- curRadians = (curDegrees * 3.14159265) / 180.0;
-
- /* determine the x coordinate */
- #ifdef _MC68881_
- x96tox80( &curRadians, &curFloatCoord );
- curFloatCoord = cos( curFloatCoord );
- #else
- curFloatCoord = cos( curRadians );
- #endif _MC68881_
-
- trigFixer = X2Fix( &curFloatCoord );
- vertices[i].h =
- vertexRadius + FixRound( FixMul( radiusFixer, trigFixer ) );
-
- /* determine the y coordinate */
- /* compensate for the “upside-downness” of the video coordinate system */
- #ifdef _MC68881_
- x96tox80( &curRadians, &curFloatCoord );
- curFloatCoord = sin( curFloatCoord );
- #else
- curFloatCoord = sin( curRadians );
- #endif _MC68881_
-
- trigFixer = X2Fix( &curFloatCoord );
- vertices[i].v =
- vertexRadius - FixRound( FixMul( radiusFixer, trigFixer ) );
- }
-
- *pointList = vertices;
-
- return errStat;
- }
-
-